<#assign baseURL = springMacroRequestContext.getContextPath() />
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>拓深科技</title>
<#include "./css.ftl">
</head>
<body>
<div id="wrapper">
<#include "./left_nav.ftl">

    <div id="page-wrapper" class="gray-bg dashbard-1">
    <#include "./top_nav.ftl">
        <div class="row wrapper border-bottom white-bg page-heading">
            <div class="col-lg-9">
                <h2>主机管理</h2>
                <ol class="breadcrumb">
                    <li>
                        消防用水管理系统
                    </li>
                    <li class="active">
                        主机管理
                    </li>
                </ol>
            </div>
        </div>
        <div class="wrapper wrapper-content">

            <div class="ibox-content">
                <div class="">
                    <div >
                        <button <#if (role_type_id > 1)>disabled</#if> type="button" style="margin-bottom: 10px;" class="btn btn-outline btn-primary" data-toggle="modal" data-target="#addModal" ><i class="fa fa-plus"></i>&nbsp;&nbsp;添加主机</button>
                    </div>
                    <div class="modal inmodal" id="addModal" tabindex="-1" role="dialog" aria-hidden="true">
                        <div class="modal-dialog">
                            <div class="modal-content animated bounceInUp">
                                <div class="modal-header">
                                    <i class="fa fa-desktop modal-icon"></i>
                                    <h4 class="modal-title">添加水压主机</h4>
                                </div>
                                <div class="modal-body">
                                    <form class="form-horizontal">
                                        <div class="form-group"><label class="col-lg-2 control-label">IP</label>
                                            <div class="col-lg-10"><input id="hostIP" type="text" placeholder="0.0.0.0" class="form-control"></div>
                                        </div>
                                        <div class="form-group"><label class="col-lg-2 control-label">端口</label>
                                            <div class="col-lg-10"><input ID="hostPORT" type="text" placeholder="502" class="form-control"></div>
                                        </div>
                                        <div class="form-group"><label class="col-lg-2 control-label">名称</label>
                                            <div class="col-lg-10"><input id="hostNAME" type="text" placeholder="水压主机命名" class="form-control"></div>
                                        </div>
                                        <div class="form-group"><label class="col-lg-2 control-label">描述</label>
                                            <div class="col-lg-10"><input id="hostDETAIL" type="text" placeholder="对主机的描述信息" class="form-control"></div>
                                        </div>
                                    </form>
                                </div>
                                <div class="modal-footer">
                                    <button id="hostCANCEL" type="button" class="btn btn-white" data-dismiss="modal"><i class="fa fa-mail-reply"></i>&nbsp;&nbsp;取消</button>
                                    <button id="hostSave" type="button" class="btn btn-primary"><i class="fa fa-check"></i>&nbsp;&nbsp;保存</button>
                                </div>
                            </div>
                        </div>
                    </div>

                    <table class="table table-striped table-bordered table-hover " id="editable" cellspacing="0" width="100%">
                        <thead>
                        <tr>
                            <th>ID</th>
                            <th>名称</th>
                            <th>IP</th>
                            <th>端口</th>
                            <th>描述</th>
                            <th>连线</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>

                        </tbody>
                    </table>

                    <div class="modal inmodal" id="updateModal" tabindex="-1" role="dialog" aria-hidden="true">
                        <div class="modal-dialog">
                            <div class="modal-content animated bounceInRight">
                                <div class="modal-header">
                                    <i class="fa fa-desktop modal-icon"></i>
                                    <h4 class="modal-title">修改水压主机</h4>
                                </div>
                                <div class="modal-body">
                                    <form id="updateForm" enctype="multipart/form-data"  class="form-horizontal" >
                                        <div class="form-group" style="display: none"><label class="col-lg-2 control-label">ID</label>
                                            <div class="col-lg-10"><input id="hostID" name="id" type="text"  class="form-control"></div>
                                        </div>
                                        <div class="form-group"><label class="col-lg-2 control-label">IP</label>
                                            <div class="col-lg-10"><input id="hostUpdateIP" name="ip" type="text"  class="form-control"></div>
                                        </div>
                                        <div class="form-group"><label class="col-lg-2 control-label">端口</label>
                                            <div class="col-lg-10"><input ID="hostUpdatePORT" name="port" type="text"  class="form-control"></div>
                                        </div>
                                        <div class="form-group"><label class="col-lg-2 control-label">名称</label>
                                            <div class="col-lg-10"><input id="hostUpdateNAME" name="name" type="text"  class="form-control"></div>
                                        </div>
                                        <div class="form-group"><label class="col-lg-2 control-label">描述</label>
                                            <div class="col-lg-10"><input id="hostUpdateDETAIL" name="detail" type="text"  class="form-control"></div>
                                        </div>
                                        <div class="form-group"><label class="col-lg-2 control-label">图片</label>
                                            <div class="col-lg-10"><input id="hostUpdatePICTURE" type="file" name="file"  class="form-control"></div>
                                        </div>
                                        <button id="updateSubmit" type="submit" style="display: none"></button>
                                    </form>
                                </div>
                                <div class="modal-footer">
                                    <button id="hostUpdateCANCEL" type="button" class="btn btn-white" data-dismiss="modal"><i class="fa fa-mail-reply"></i>&nbsp;&nbsp;取消</button>
                                    <button id="hostUpdateSave" type="button" class="btn btn-primary"><i class="fa fa-check"></i>&nbsp;&nbsp;保存</button>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>


        </div>
    <#include "./bottom_nav.ftl">
    </div>

</div>


<#include "./js.ftl">

<script>

    var server = getURL() + "${baseURL}";

    var dTable = $('#editable').DataTable({
        language: {
            "sProcessing": "处理中...",
            "sLengthMenu": "显示 _MENU_ 项结果",
            "sZeroRecords": "没有匹配结果",
            "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
            "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
            "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
            "sInfoPostFix": "",
            "sSearch": "搜索:",
            "sUrl": "",
            "sEmptyTable": "表中数据为空",
            "sLoadingRecords": "载入中...",
            "sInfoThousands": ",",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上页",
                "sNext": "下页",
                "sLast": "末页"
            },
            "oAria": {
                "sSortAscending": ": 以升序排列此列",
                "sSortDescending": ": 以降序排列此列"
            }
        },
        ajax: "${baseURL}/water/device/getAll",
        columns:[
            {"data":"id"},
            {"data":"device_name"},
            {"data":"device_ip"},
            {"data":"port"},
            {"data":"detail"},
            {"data":"online","render": function ( data, type, row, meta ) {
                if(data === 0){
                    return '<button <#if (role_type_id > 2)>disabled</#if> type="button" class="btn btn-block btn-danger" onclick="online('+ row.id +')"  style="padding: 0 0 0 0">离&nbsp;&nbsp;线</button>';
                }
                else{
                    return '<button <#if (role_type_id > 2)>disabled</#if> type="button" class="btn btn-block btn-primary" onclick="offLine('+ row.id +')" style="padding: 0 0 0 0">在&nbsp;&nbsp;线</button>';
                }
            }},
            {"data":null,"render": function ( data, type, row, meta ) {
                     var obj = encodeURIComponent(JSON.stringify(row));
                     return   '<div  class="col-sm-12" style="padding: 0 0 0 0;"><div class="col-sm-6" style="padding: 0 2px 0 0;"><button <#if (role_type_id > 1)>disabled</#if> data-toggle="modal" data-target="#updateModal" onclick='+"editBtn("+'"'+obj+'"'+")"+ ' type="button" class="btn btn-block btn-success" style="padding: 0 0 0 0;"><i class="fa fa-pencil-square-o"></i>&nbsp;&nbsp;编&nbsp;辑</button></div> <div class="col-sm-6" style="padding: 0 2px 0 0;"><button <#if (role_type_id > 1)>disabled</#if> onclick="deleteBtn('+row.id+')" type="button" class="btn btn-block btn-warning" style="padding: 0 0 0 0;"><i class="fa fa-trash"></i>&nbsp;&nbsp;删&nbsp;除</button></div></div>'
            },"width":"15%",targets:6}
        ],
        aoColumnDefs: [ { "bSortable": false, "aTargets": [ 6 ] }],
        destroy: true
    });

    function editBtn(obj){
        var data = JSON.parse(decodeURIComponent(obj));
        $("#hostID").val(data.id);
        $("#hostUpdateIP").val(data.device_ip);
        $("#hostUpdatePORT").val(data.port);
        $("#hostUpdateNAME").val(data.device_name);
        $("#hostUpdateDETAIL").val(data.detail);
    };

    $("#hostUpdateSave").click(function(){
        var form = new FormData(document.forms.namedItem("updateForm"));
        $.ajax({
            url: "${baseURL}/water/device/update2",
            contentType: false,
            cache: false,
            processData: false,
            type: 'POST',
            dataType: 'json',
            data: form,
            success:function(data){
                if(data.code === 0) {
                    swal({
                        title: "操作成功",
                        type: "success"
                    }, function () {
                       location.reload();
                    });
                }
            },
            error:function(error) {
                swal({
                    title: "提交失败，请检查输入的内容。",
                    type: "error"
                })
            }
        });

    });

    function deleteBtn(id){
        $.ajax({
            type: "POST",
            contentType: "application/json",
            dataType: "json",
            url: "${baseURL}/water/device/delete?id=" + id,
            success:function(data){
                if(data.code === 0){
                    swal({
                        title: "删除成功。",
                        type: "success"
                    });
                    dTable.ajax.reload();
                }
                else{
                    swal({
                        title: "操作失败。",
                        type: "error"
                    })
                }
            },
            error:function(xhr, status, error){
                swal({
                    title: "操作失败。",
                    type: "error"
                })
            }
        });
    };

    function online(id){
        $.ajax({
            type: "GET",
            contentType: "application/json",
            dataType: "json",
            url: "${baseURL}/water/device/online?id=" + id,
            success:function(data){
                if(data.code === 0){
                    dTable.ajax.reload();
                }
                else{
                    swal({
                        title: "操作失败。",
                        type: "error"
                    })
                }
            },
            error:function(xhr, status, error){
                swal({
                    title: "操作失败。",
                    type: "error"
                })
            }
        });
    }

    function offLine(id){
        $.ajax({
            type: "GET",
            contentType: "application/json",
            dataType: "json",
            url: "${baseURL}/water/device/offLine?id=" + id,
            success:function(data){
                if(data.code === 0){
                    dTable.ajax.reload();
                }
                else{
                    swal({
                        title: "操作失败。",
                        type: "error"
                    })
                }
            },
            error:function(xhr, status, error){
                swal({
                    title: "操作失败。",
                    type: "error"
                })
            }
        });
    };

    $("#hostSave").click(function(){
        var ip = $("#hostIP").val();
        var port = $("#hostPORT").val();
        var name = $("#hostNAME").val();
        var detail = $("#hostDETAIL").val();
        console.log({"device_name":name, "device_ip":ip,"port":port,"detail":detail});
        $.ajax({
            type: "POST",
            contentType: "application/json",
            dataType: "json",
            url: "${baseURL}/water/device/add",
            data: JSON.stringify({"device_name":name, "device_ip":ip,"port":port,"detail":detail}),
            success:function(data){
                if(data.code === 0){
                    swal({
                        title: "操作成功",
                        type: "success"
                    },function(){
                        $("#hostCANCEL").click();
                        dTable.ajax.reload();
                    });
                }
                else{
                    swal({
                        title: data.data,
                        type: "error"
                    })
                }
            },
            error:function(xhr, status, error){
                swal({
                    title: "提交失败，请检查输入的内容。",
                    type: "error"
                })
            }
        });

    });

    $(function(){
        if ("WebSocket" in window)
        {
            var ws = new WebSocket("ws://"+ server +"/socketDevice");
            ws.onopen = function()
            {
                console.log(ws);
            };
            ws.onmessage = function (evt)
            {
                var received_msg = evt.data;
                console.log("on message: " + received_msg);
                dTable.ajax.reload();
            };
            ws.onclose = function()
            {
               console.log("Websocket Connection is closed...");
            };
            ws.onerror = function (err)
            {
                console.log(err);
            }
        }
        else
        {
            alert("你的浏览器不支持WebSocket,请更换高版本Chrome");
        }
    });



</script>
</body>
</html>